/*
 * queueTest.c
 *
 *  Created on: Apr 9, 2011
 *      Author: shaican
 */
#include "types.h"
#include "user.h"

struct queue{
	int processes[64];
	int size;
};

void initQueue(struct queue* waiters){
//	struct proc* p;
//	for(p = waiters->processes; p < &waiters->processes[NPROC]; p++){
//		p = 0;
//	}
//	waiters->size = 0;
	int i;
	for(i = 0; i < 64; i++){
		waiters->processes[i] = 0;
	}
	waiters->size = 0;
}

void enqueue(int p, struct queue* waiters){
	waiters->processes[waiters->size] = p;
	waiters->size++;
}

int dequeue(struct queue* waiters){
	int returnProc = -1;
	int i = 0;
	if(waiters->size <= 0){
		printf(1,"queue is empty");
	}
	else{
		returnProc = waiters->processes[0];
		for(i = 1; i < waiters->size; i++){
			waiters->processes[i-1] = waiters->processes[i];
		}
		waiters->size--;
	}
	return returnProc;
}

void printPID(struct queue* waiters){
	int i;
	for(i = 0; i < waiters->size; i++){
		printf(1, "%d\n", waiters->processes[i]);
	}
}

int main(void){
	struct queue* waiters = malloc(sizeof(struct queue));
	initQueue(waiters);
	int p3 = 3;
	int p4 = 4;
	int p5 = 5;
	enqueue(p3, waiters);
	enqueue(p4, waiters);
	printPID(waiters);
	dequeue(waiters);
	printPID(waiters);
	enqueue(p5, waiters);
	printPID(waiters);
	free(waiters);
	exit();
	return 0;
}
